FIELD Statement ---------------------------------------------------------------------------- Action Allocates space for variables in a random-access file buffer. Syntax FIELD -#- filenumber%, fieldwidth% ASstringvariable$ -, fieldwidth% ASstringvariable$-... Remarks The FIELD statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- filenumber% The number used in the OPEN statement to open the file. fieldwidth% The number of characters in a field. stringvariable$ The string variable that contains the data read from a record, or data that is used in an assignment when information is written to a record. The total number of bytes that you allocate in a FIELD statement must not exceed the record length you specified when opening the file. Otherwise, BASIC generates an error message that reads FIELD overflow. (The default record length is 128 bytes.) Any number of FIELD statements can be executed for the same file. All FIELD statements that have been executed remain in effect at the same time. All field definitions for a file are removed when the file is closed; that is, all strings defined as fields associated with the file are set to null. Note Do not use a variable name defined as a field in an INPUT or assignment statement if you want the variable to remain a field. Once a variable name is a field, it points to the correct place in the random-access file buffer. If a subsequent INPUT or assignment statement with that variable name is executed, the variable's pointer no longer refers to the random-access record buffer, but to string space. BASIC's record variables and extended OPEN statement syntax provide a more convenient way to use random-access files. See Chapter 3, "File and Device I-O" in the Programmer's Guide for an extended discussion of using record variables for file I-O. BASICA When a random-access file is closed with a CLOSE or RESET statement in a compiled program, all variables that are fields associated with that file are reset to null strings. When a random-access file is closed in a BASICA program, variables that are fields retain the last value assigned to them by a GET statement. See Also GET (File I-O), LSET, PUT (File I-O), RSET Example The example below illustrates a random-access file buffer with multiple definitions. In the first FIELD statement, the 67-byte buffer is broken up into five separate variables for name, address, city, state, and zip code. In the second FIELD statement, the same buffer is assigned entirely to one variable, Plist$. The remainder of this example checks to see if Zip$, which contains the zip code, falls within a certain range; if it does, the complete address string is printed. TYPE Buffer FuName AS STRING * 25 Addr AS STRING * 25 City AS STRING * 10 State AS STRING * 2 Zip AS STRING * 5 END TYPE DIM RecBuffer AS Buffer ' This part of the program creates a random-access file for use by the ' second part of the program, which demonstrates the FIELD statement. OPEN "MAILLIST.DAT" FOR RANDOM AS #1 LEN = LEN(RecBuffer) CLS RESTORE READ FuName$, Addr$, City$, State$, Zip$ I = 0 DO WHILE UCASE$(FuName$) <> "END" I = I + 1 RecBuffer.FuName = FuName$ RecBuffer.Addr = Addr$ RecBuffer.City = City$ RecBuffer.State = State$ RecBuffer.Zip = Zip$ PUT #1, I, RecBuffer READ FuName$, Addr$, City$, State$, Zip$ IF FuName$ = "END" THEN EXIT DO LOOP CLOSE #1 DATA "Bob Hartzell","1200 Liberty St.","Bow","WA","98232" DATA "Alice Provan","123 B St.","Bellevue","WA","98005" DATA "Alex Landow","14900 123rd","Bothell","WA","98011" DATA "Walt Riley","33 Minnow Lake Road","Lyman","WA","98263" DATA "Georgette Gump","400 15th W.","Bellevue","WA","98007" DATA "END",0,0,0,0,0 ' Define field and record lengths with constants. CONST FU = 25, AD = 25, CT = 10, ST = 2, ZP = 5 CONST RECLEN = FU + AD + CT + ST + ZP OPEN "MAILLIST.DAT" FOR RANDOM AS #1 LEN = RECLEN FIELD #1, FU AS FuName$, AD AS Addr$, CT AS City$, ST AS State$, ZP AS Zip$ FIELD #1, RECLEN AS Plist$ GET #1, 1 ' Read the file, looking for zip codes in the range 98000 to 98015. DO WHILE NOT EOF(1) Zcheck$ = Zip$ IF (Zcheck$ >= "98000" AND Zcheck$ <= "98015") THEN Info$ = Plist$ PRINT LEFT$(Info$, 25) PRINT MID$(Info$, 26, 25) PRINT RIGHT$(Info$, 17) PRINT END IF GET #1 LOOP CLOSE #1